
#ifndef _TESTSCREEN
#define _TESTSCREEN

#include <CEGUI/CEGUI.h>
#include <OIS/OIS.h>
#include <OgreCEGUIRenderer.h>
#include "GUI/CEGUICommonFileDialog.h"

class TestScreen : public Screen
{
public:
	TestScreen(){}
	TestScreen(String  layoutFile):Screen(layoutFile)
	{
		_mlstListbox = (CEGUI::Listbox*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/List/Listbox");
		mainWindow = CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root");
		mainWindow->addChildWindow(CEGUI::CommonFileDialog::getSingleton().getWindow());
	}
	// Events
	void subscribeEvents()
	{
		CEGUI::Event::Connection connect;

		CEGUI::PushButton* _mBtnLoad = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Load/LoadButton");
		CEGUI::PushButton* _mBtnSave = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().getWindow((CEGUI::utf8*)"Root/Save/SaveButton");
		
		connect = _mBtnLoad->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&TestScreen::handleLoad, this));
		mEventConnections.push_front(connect);
		connect = _mBtnSave->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&TestScreen::handleSave, this));
		mEventConnections.push_front(connect);

		connect = CEGUI::CommonFileDialog::getSingleton().getWindow()->subscribeEvent (CEGUI::FrameWindow::EventHidden, CEGUI::Event::Subscriber(&TestScreen::handleFileDialogOutput, this));
		mEventConnections.push_front(connect);
	}
	// MouseListener
    bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id){return true;}
    // KeyListener
    bool keyPressed(const OIS::KeyEvent &arg)
	{
		if (arg.key == OIS::KC_ESCAPE)
			mContinue=false;
		return true;
	}
	
private:
	bool handleLoad(const CEGUI::EventArgs& e)
	{
		/****************************************************************************************************
		Step 3a: Call the file dialog with the input data
		*****************************************************************************************************/
		CEGUI::CommonFileDialogInput input;
		input.setHandle (1);										// Set the handle to determine the originator
		input.setFilter ("Archive (*.zip)|*.zip|All files|*.*|");	// Extensions
		input.setOpenFileDialog (true);								// Load mode
		input.setDefaultExtension ("*.zip");						// Zip is the first one to display
		CEGUI::CommonFileDialog::getSingleton().openDialog(input);	// Open the common file dialog
		return true;
	}

	bool handleSave(const CEGUI::EventArgs& e)
	{
		/****************************************************************************************************
		Step 3b: Call the file dialog with the input data
		*****************************************************************************************************/
		CEGUI::CommonFileDialogInput input;
		input.setHandle (2);										// Set the handle to determine the originator
		input.setFilter ("JPEG (*.jpg)|*.jpg|Portable Network Graphics (*.png)|*.png|Truevision Targa (*.tga)|*.tga|");
		input.setOpenFileDialog (false);							// Save mode
		input.setDefaultExtension ("*.png");						// .png is the preferred extension
		input.setFileName ("test.png");								// Set default filename
		CEGUI::CommonFileDialog::getSingleton().openDialog(input);	// Open the common file dialog
		return true;
	}

	bool handleFileDialogOutput(const CEGUI::EventArgs& e)
	{
		/****************************************************************************************************
		Step 4: Handle the returned output
		*****************************************************************************************************/
		CEGUI::CommonFileDialogOutput result = CEGUI::CommonFileDialog::getSingleton().getResult();
		switch (result.getHandle())
		{
			case 1:
				_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Load button pressed"));
			break;

			case 2:
				_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Save button pressed"));
			break;
		}
		
		switch (result.getAction())
		{
			case CEGUI::ACTION_OK:
				_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_OK"));
			break;

			case CEGUI::ACTION_CANCEL:
				_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_CANCEL"));
			break;

			case CEGUI::ACTION_WINDOW_CLOSED:
				_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Returned with ACTION_WINDOW_CLOSED"));
			break;
		}
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Drive: " + result.getDrive()));
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Full qualified name: " + result.getFullQualifiedFileName()));
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Absolute path: " + result.getAbsolutePath()));
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Relative path: " + result.getRelativePath()));
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"Filename: " + result.getFileName()));
		CEGUI::String fileExists = result.isFileExisting()? "TRUE" : "FALSE";
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)fileExists.c_str()));
		_mlstListbox->addItem(new CEGUI::ListboxTextItem ((CEGUI::utf8*)"-------------------------------------------------------------------------------"));
		return true;
	}

	CEGUI::Listbox* _mlstListbox;
	CEGUI::Window* mainWindow;
};

#endif

	